package com.aoasaa;

import com.userrequestdtos.CreateCategoryDTO;
import com.userrequestdtos.ViewingCategoryAndTitleDTO;
import com.userutils.WSConnectionFile;
import com.userutils.WSConnectionStrings;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class CreateVoiceNotes extends Activity implements OnItemSelectedListener {
	EditText title;
	static String[] categorylist={"h","k"};
	public SharedPreferences sp_obj;
	public Editor editor_obj;
	Spinner sp; 
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.creatingaudioscreen);
	        
	        // accessing shared preference       
			sp_obj=getSharedPreferences("ArchiveApp",MODE_WORLD_WRITEABLE);
			editor_obj=sp_obj.edit();

			// creating reference for spinner
					sp=(Spinner)findViewById(R.id.spinner_audio);
					  
			// fetching user categories 
			ViewingCategoryAndTitleDTO obj_viewcategorytitle =new ViewingCategoryAndTitleDTO("sharan","nonote");
			// assigning the object values, object names and method name
			WSConnectionStrings.parameterobjectvalue=obj_viewcategorytitle;
			WSConnectionStrings.objectname="obj_viewcategorytitle"; 
			WSConnectionStrings.USER_METHOD_NAME=WSConnectionStrings.Method_viewCategories;
			
			new MyAsyncTask123().execute();
	 
			
	        /*when clicked on record notes button, 
	        navigate to recording screen*/
	        Button record_notes=(Button)findViewById(R.id.btn_recordingscreen);
	        record_notes.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					title=(EditText)findViewById(R.id.editText_audiotag);
					editor_obj.putString("selectedfilecategory",sp.getSelectedItem().toString()); 
					editor_obj.putString("selectedfiletitle", title.getEditableText().toString());
					editor_obj.commit();
					 Intent i=new Intent(CreateVoiceNotes.this,VoiceRecorderActivity.class);
				     startActivity(i);  
				}
			});
	       
	        
	    } // closing oncreate
	// inner class  For Network Connection i.e, for Web service connection
		class MyAsyncTask123 
		extends AsyncTask<Void, String, String>
		{
			@Override
			protected void onPostExecute(String response) 
			{
	              // If response comes while viewing category
				if(WSConnectionStrings.objectname.equals("obj_viewcategorytitle")) 
				{

					categorylist=UserXMLParsingFile.responseOperation(CreateVoiceNotes.this,response, "", "categoryname");
					Log.i("categorylist", ""+categorylist);

					// adding array to spinner  
					sp.setAdapter(new ArrayAdapter(CreateVoiceNotes.this, android.R.layout.simple_list_item_1, categorylist));
					sp.setOnItemSelectedListener(CreateVoiceNotes.this);
					Log.i("categorylistcheck", ""+categorylist); 
				}      
				// if response comes while creating a category
				else if(WSConnectionStrings.objectname.equals("obj_createcategory"))
				{
					 String[] res=UserXMLParsingFile.responseOperation(CreateVoiceNotes.this,response, "", "");
				     if(res[0].equals("created category"))
				     {
				    	 ViewingCategoryAndTitleDTO obj_viewcategorytitle =new ViewingCategoryAndTitleDTO("sharan","nonote");
				    	// assigning the object values, object names and method name
				 		WSConnectionStrings.parameterobjectvalue=obj_viewcategorytitle;
				 		WSConnectionStrings.objectname="obj_viewcategorytitle"; 
				 		WSConnectionStrings.USER_METHOD_NAME=WSConnectionStrings.Method_viewCategories;
				 		new MyAsyncTask123().execute();
				     }
				}	
			}
			@Override 
			protected String doInBackground(Void... params) {
				// your network operation
				String wsresponse=WSConnectionFile.wsConn(WSConnectionStrings.parameterobjectvalue, WSConnectionStrings.objectname, 
						WSConnectionStrings.USER_METHOD_NAME,
						WSConnectionStrings.NAMESPACE+WSConnectionStrings.USER_METHOD_NAME);
				return wsresponse;   
			}        
		}
		@Override
		public void onItemSelected(AdapterView<?> arg0, View v, int position,
				long arg3) 
		{
			if(position==0)
			{
				final EditText et=new EditText(v.getContext());
				AlertDialog.Builder alert=new AlertDialog.Builder(v.getContext());
				alert.setTitle("Create a new category");
				alert.setMessage("Enter the category name");
				alert.setView(et);

				alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub

					}   
				});
				alert.setPositiveButton("Create", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Log.i("giventext", et.getEditableText().toString());
						CreateCategoryDTO obj_createcategory=new CreateCategoryDTO("sharan",et.getEditableText().toString()); 
						
						// assigning the object values, object names and method name
						WSConnectionStrings.parameterobjectvalue=obj_createcategory;
						WSConnectionStrings.objectname="obj_createcategory";
						WSConnectionStrings.USER_METHOD_NAME=WSConnectionStrings.METHOD_createUserCategories;
						new MyAsyncTask123().execute();
					}
				});
				alert.show();
			} 
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
}
